package exhibition.app.nexd.cn.ui.activity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.content.res.ObbInfo;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.OnClick;
import exhibition.app.nexd.cn.R;
import exhibition.app.nexd.cn.adapter.HireListAdapter;
import exhibition.app.nexd.cn.presenter.MainPresenter;
import exhibition.app.nexd.cn.ui.ivew.IMainActivityView;
import exhibition.app.nexd.cn.widget.BadgeView;


public class MainActivity extends BaseActivity<MainPresenter> implements IMainActivityView {

    @Bind(R.id.hire_lv_list)
    ListView lvHire;
    @Bind(R.id.hire_iv_cart)
    ImageView ivCart;
    @Bind(R.id.hire_tv_price)
    TextView tvPrice;
    @Bind(R.id.hire_tv_pay)
    TextView tvPay;

    private HireListAdapter hireListAdapter;

    private BadgeView redDot;

    int[] shopCarLocation;

    @Override
    public void initView() {
        getSupportActionBar().hide();
        redDot = new BadgeView(this);
        presenter.reCalculation();
        hireListAdapter = new HireListAdapter(this);
        lvHire.setAdapter(hireListAdapter);
        //增减商品拿到最新的订单信息刷新界面
        hireListAdapter.setOnUpdateDataListener(new HireListAdapter.OnUpdateDataListener() {
            @Override
            public void onUpdate() {
                presenter.reCalculation();
                //AnimationSet scaleSet = new AnimationSet(false);
                //添加购物车购物车图标动画
                AnimatorSet animator = new AnimatorSet();
                ObjectAnimator scaleX = ObjectAnimator.ofFloat(ivCart, "scaleX", 1.0f, 0.8f, 1.0f);
                ObjectAnimator scaleY = ObjectAnimator.ofFloat(ivCart, "scaleY", 1.0f, 0.8f, 1.0f);
                animator.setInterpolator(new AccelerateDecelerateInterpolator());
                animator.setStartDelay(800);
                animator.setDuration(200);
                animator.playTogether(scaleX, scaleY);
                animator.start();

            }
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_hire;
    }

    @Override
    protected void initPresenter() {
        presenter = new MainPresenter(this, this);
        presenter.init();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        //获取购物车图标的xy坐标（添加购物车抛物线动画的endLocation）
        super.onWindowFocusChanged(hasFocus);
        shopCarLocation = new int[2];
        ivCart.getLocationInWindow(shopCarLocation);
        hireListAdapter.setShopCarLocation(shopCarLocation);
    }


    @Override
    public void showToast(String msg) {
        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.hire_tv_pay)
    public void onBtnClick(View v) {
        presenter.payOrder();
    }

    @Override
    public void updateGoods(int totalCount, double totalPrice) {
        redDot.setTargetView(ivCart);
        redDot.setBadgeCount(totalCount);
        tvPrice.setText("总价:￥" + totalPrice);
    }

    @Override
    public void onPayOrderSuccess() {
        Intent intent = new Intent(MainActivity.this, OrderFormDetailActivity.class);
        startActivity(intent);
    }

    public MainActivity() {

    }
}
